/*评论的代码*/

LookerComment = function(newshash,total) 
{
    this.ListURL = "http://localhost/looker/looker/index.php/Comment/show/";
    this.AddURL  = "http://localhost/looker/looker/index.php/Comment/add/";
    this.ListLimit = 10;
    this.NewsHash  = newshash;
    this.TotalCnt  = total;
    this.CurPage   = 1;
    this.CurNumber = 0;
    this.IsLoad    = false;
    this.IsShow    = false;
    this.ScrollTo  = 0;
    this.LoadingImg = "http://localhost/looker/looker/Tpl/simpleX/Public/images/ajaxloading.gif"; 
    this.PageNavDiv = "pagenav";
    this.CommentTpl = "comment_post_tpl";
    this.MessageDiv = "comment_message";
    this.CommentListDiv = "comment_posts";
    this.CommentForm = {
                author:"cmt_form_author",
                content:"cmt_form_content",
                verify:"cmt_form_verify",
                submit:"cmt_form_submit"};

    this.PageNavOpt = {
		items_per_page: this.ListLimit,
		num_display_entries:10,
		current_page:0,
		num_edge_entries:0,
		link_to:"#",
		prev_text:"上页",
		next_text:"下页",
		ellipse_text:"...",
		prev_show_always:true,
		next_show_always:true,
        commentHander:this,
		callback:function(page_index, jq){
            this.commentHander.display(page_index+1);
            return false;
            }
    
    };

    
    //分页导航对象
    this.pageNav = null;


    //显示一页的评论
    this.display = function(page){
        this.loading("正在加载评论，请稍候。。。");
        $.scrollTo("#comment_top");
        $("#"+this.CommentListDiv).hide();
        this.getData(page);
        this.isLoad = true;
        
    };

    //添加一条评论
    this.add = function(){
        if( !this.check())
            return;
        this.loading("正在提交评论，请稍候...");

        $("#"+this.CommentForm.submit).attr("disabled","true");
        this.addData();
    };

    //从服务器获取一页评论
    this.getData = function(page){
        var self = this;
        var sucfn = function(data){
            if(data.status==0){
                self.showMessage(data.info,0, 5000 );
            }else{
                 self.loading("正在渲染评论，请稍候...");
                 self.TotalCnt  = parseInt(data.data.info.total);
                 self.ListLimit = parseInt(data.data.info.limit);
                 self.CurPage   = parseInt(data.data.info.page);

                 if(!self.pageNav && self.TotalCnt){
                    self.pageNav = $("#"+self.PageNavDiv).pagination(self.TotalCnt, self.PageNavOpt);
                 }
 
            if(data.data.data==false){
                self.showMessage("暂时还没有评论哟~~",1, 0);
                $("#"+self.CommentListDiv).show();
                return;
            }
                 self.render(data.data.data);
            }
        };

        var data = {"p":page, "news":this.NewsHash};
         //向服务器提交添加评论请求
        $.ajax({
            "type" : "POST",
            "url"  : this.ListURL,
            "dataType" : "json",
            "data" : data,
            "success" : sucfn
        });
    };
    //渲染这一页评论
    this.render = function(data){

        var tpl = $("#"+this.CommentTpl).html();
        var postListHtml = "";
        this.CurNumber = (this.CurPage-1)*this.ListLimit;
        for(i=0; i<data.length; i++){
            var curPost = data[i];
            var postHtml = tpl.replace("{commentid}",curPost.commentid);
            postHtml = postHtml.replace("{author}", curPost.author);
            postHtml = postHtml.replace("{content}", curPost.content);
            postHtml = postHtml.replace("{date}", this.formatDate(curPost.date));
            postHtml = postHtml.replace("{number}", ++this.CurNumber);
            postHtml = postHtml.replace("{number2}", this.CurNumber);

            postListHtml += postHtml;
        }
        var div =  $("#"+this.CommentListDiv);
        div.html(postListHtml);

        if(!this.IsShow)
            this.toggle();
        if(this.ScrollTo){
              div.show();
              $.scrollTo("#comment_"+this.ScrollTo,800);
              this.ScrollTo = 0;
        }
        else{
             div.slideDown();
        }
     this.showMessage("加载评论成功~~", 1 ,3000);
     this.hideMessage();
    };

    //添加一条评论
    this.append = function (curPost){
         var postHtml = $("#"+this.CommentTpl).html().replace("{commentid}",curPost.commentid);
        postHtml = postHtml.replace("{author}", curPost.author);
        postHtml = postHtml.replace("{content}", curPost.content);
        postHtml = postHtml.replace("{date}", this.formatDate(curPost.date));
        postHtml = postHtml.replace("{number}", ++this.CurNumber);
        postHtml = postHtml.replace("{number2}", this.CurNumber);
        $("#"+this.CommentListDiv).append(postHtml);


        if(!this.IsShow)
            this.toggle();
        //滚动新添的评论
        $.scrollTo("#comment_"+curPost.commentid,800);
        this.ScrollTo = 0;
    };

    //格式化时间
    this.formatDate = function(dateStr){
        var date = new Date(parseInt(dateStr)*1000);
        var ret  = date.getFullYear()+'/'+(date.getMonth()+1)+'/'+date.getDate()+' '+date.getHours()+':'+date.getMinutes();
        return ret;
    };

    //检查用户输入
    this.check = function(){
        var err = "";
        if($("#"+this.CommentForm.author).attr("value").length==0)
            err +="请填写你的名字!<br />";

        if($("#"+this.CommentForm.content).attr("value").length==0)
            err +="请填写你的评论内容!<br />";

        
        if($("#"+this.CommentForm.verify).attr("value").length==0)
            err +="请填写验证字符!<br />";

        if(err.length>0){
            this.showMessage(err, 0 , 5000);
            return false;
        }
        else{
            return true;
        }
    };
    //显示载入画面
    this.loading = function(msg){
        var html = "<img src='"+this.LoadingImg+"' alt='加载中...' />"+msg;
        var div = $("#"+this.MessageDiv);
        div.html(html);
        div.show();
    };

    //向服务器添加一条评论
    this.addData = function(){
        var data = {
            "author"  : $("#"+this.CommentForm.author).attr("value"),
            "content" : $("#"+this.CommentForm.content).attr("value"),
            "verify"  : $("#"+this.CommentForm.verify).attr("value"),
            "newshash": this.NewsHash
        };
        var self = this;  //闭包。。。
        var sucfn = function(data){
            if(data.status==1){
                self.showMessage('恭喜，评论成功~~',1,5000);
                $("#"+self.CommentForm.content).attr("value","");
                $("#"+self.CommentForm.verify).attr("value","");
                fleshVerify();

                self.TotalCnt++;
                $(".comment_count").html(self.TotalCnt);
                var lastPage = Math.ceil(self.TotalCnt/self.ListLimit);
                self.ScrollTo = data.data.commentid;
                //将新的评论加载到列表中
                if( self.CurPage==lastPage && self.CurPage*self.ListLimit>=self.TotalCnt ){
                    if(self.isLoad)
                        self.append(data.data);
                    else{
                        self.display(self.CurPage);
                    }
                }
                else{
                    self.PageNavOpt.current_page = lastPage-1;
                    self.pageNav = $("#"+self.PageNavDiv).pagination(self.TotalCnt, self.PageNavOpt);
                    self.display(lastPage);
                }
                
            }else{
                self.showMessage("出错啦，"+data.info, 0 , 5000);
            }
             $("#"+self.CommentForm.submit).removeAttr("disabled");

        };
        //向服务器提交添加评论请求
        $.ajax({
            "type" : "POST",
            "url"  : this.AddURL,
            "dataType" : "json",
            "data" : data, 
            "success" : sucfn
        });
    };

    //显示提示信息
    this.showMessage = function(msg, status, interval){
        var html = "";
        if(status==0)
            html = "<font color='red'>"+msg+"<font>";
        else
            html = "<font color='blue'>"+msg+"<font>";

        var div = $("#"+this.MessageDiv);
        div.html(html);
        div.show();
        if(interval!=0)
             div.fadeOut(interval);
    };

    this.hideMessage = function(){
          $("#"+this.MessageDiv).fadeOut(5000);
    };

    //回复评论
    this.reply = function(number){
        var replyStr = "回复 #"+number+"楼:\n---------------------------\n";
        var contentForm = $("#"+this.CommentForm.content);
        contentForm.attr("value",replyStr);
        contentForm.focus();
        //滚动到表格
        $.scrollTo("#"+this.CommentForm.author, 800);
    };

    //滚动到顶部
    this.top = function(){
         $.scrollTo("#comment_top",800);
    };

    //展开评论
    this.show = function()
	{
		if(!this.isLoad){
			this.display(1);
		}
        this.IsShow = this.IsShow ? false : true;
         $("#comment_post_area").slideToggle(800);
	};

    //
    this.toggle = function(){
         this.IsShow ? $("#comment_post_area").hide() :  $("#comment_post_area").show();
         this.IsShow = this.IsShow ? false : true;
    }
};